package com.wisdytech.linkdcs.equipLifeCycle.maintain.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenanceType;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.service.IMaintenanceTypeService;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.vo.MaintenanceTypeVO;
import com.wisdytech.linkdcs.system.model.SysUser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  保养类别前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-06-09
 */
@Controller
@RequestMapping("/maintain/maintenanceType")
public class MaintenanceTypeController {

	@Autowired
	private IMaintenanceTypeService iMaintenanceTypeService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	/**
     * 
     * <p>Description: 设备保养类别</p>
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String maintainType(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "lifeCycle/equipLifeCycle/maintain/type";
    }
    
    /**
     * 分页条件查询保养类别及其关联的保养项信息
     * @param typeCode 备件编码
     * @param typeName 备件名称
     * @param filter 分页参数
     * @return 保养类别列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listTypeAndItemByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<MaintenanceTypeVO> listTypeAndItemByFilter(String typeCode, String typeName,Filter filter) throws IOException{
    	PageInfo<MaintenanceType> types =this.iMaintenanceTypeService.listTypeAndItemByFilter(typeCode, typeName,filter);
        //转换为前台结果集
        List<MaintenanceType> list=types.getList();
        List<MaintenanceTypeVO> VOList=new ArrayList<MaintenanceTypeVO>();
        for(MaintenanceType maintenanceType:list){
        	VOList.add(DataTransformUtil.changeTypeAndItemVO(maintenanceType));
        }
        return new StandardTableResult<MaintenanceTypeVO>(types.getTotal(),VOList);
    }
    
    /**
     * <p>Description: 保存保养类别对象</p> 
     * @param maintenanceType 保养项对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveType/save.mvc")
    @ResponseBody
    public StandardResult saveType(MaintenanceType maintenanceType, HttpSession session) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
        if (StringUtils.isBlank(maintenanceType.getPkId())) {
        	iMaintenanceTypeService.addType(maintenanceType, loginName);
        }else {
        	iMaintenanceTypeService.updateType(maintenanceType, loginName);
        }
        return StandardResult.getSuccessResult();
    }
	
    /**
     * <p>Description:查询保养类别信息</p> 
     * @param pkid 
     * @return 保养类别信息
     * @throws IOException 
     */
    @RequestMapping(value = "/getTypeById/get.mvc")
    @ResponseBody
    public StandardResult getTypeById(String pkId){
    	MaintenanceType maintenanceType = iMaintenanceTypeService.getTypeById(pkId);
        return StandardResult.getSuccessResult(maintenanceType);
    }
    
    
    
    /**
     * 通过id批量删除保养类别信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/delTypeById/del.mvc")
    @ResponseBody
    public StandardResult delTypeById(String pkId) {
    	iMaintenanceTypeService.delTypeById(pkId);
        return StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询保养类别编码信息列表</p> 
     * @param search 模糊查询
     * @param modelCode 设备型号
     */
    @RequestMapping(value = "/listTypeAllBySearch/get.mvc")
    @ResponseBody
    public StandardResult listTypeAllBySearch(String search,String modelCode){
        List<MaintenanceType> list = iMaintenanceTypeService.listTypeAllBySearch(search,modelCode);
        return StandardResult.getSuccessResult(list);
    }
}

